<template>
    <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="70px" @submit.native.prevent>
            <el-form-item label="学校名称" prop="name">
                <el-input v-model="queryParams.name" class="!w-240px" clearable placeholder="请输入学校名称" @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="所属省份" prop="province">
                <el-select v-model="queryParams.province" placeholder="请选择省份" filterable default-first-option clearable>
                    <el-option v-for="item in regionData" :label="item.label" :value="item.label"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleQuery">
                    <Icon class="mr-5px" icon="ep:search"/>
                    搜索
                </el-button>
                <el-button @click="resetQuery">
                    <Icon class="mr-5px" icon="ep:refresh"/>
                    重置
                </el-button>
                <el-button v-hasPermi="['yxt:school:create']" plain type="primary" @click="openForm('create')">
                    <Icon class="mr-5px" icon="ep:plus"/>
                    新增
                </el-button>
            </el-form-item>
        </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="学校名称" prop="name" min-width="150"/>
            <el-table-column label="所属省份" prop="province" min-width="120"/>
            <el-table-column label="地址" prop="address" min-width="250"/>
            <el-table-column label="经纬度" prop="location" min-width="180"/>
            <el-table-column label="备注" prop="remark" min-width="250"/>
            <el-table-column label="图片" prop="picture" width="50">
                <template #default="scope">
                    <div style="width: 36px; height: 36px">
                        <el-image v-if="scope.row.picture" :src="scope.row.picture" :preview-src-list="[scope.row.picture]" preview-teleported hide-on-click-modal/>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="状态" prop="status">
                <template #default="scope">
                    <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="人脸识别" prop="isFace">
                <template #default="scope">
                    <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.isFace"/>
                </template>
            </el-table-column>
            <el-table-column :formatter="dateFormatter" align="center" label="创建时间" prop="createTime" width="180"/>
            <el-table-column :width="200" align="center" label="操作" fixed="right">
                <template #default="scope">
                    <el-button v-hasPermi="['yxt:school:supplier:update']" link type="primary" @click="openSupplier(scope.row.id)">
                        硬件配置
                    </el-button>
                    <el-button v-hasPermi="['yxt:school:update']" link type="primary" @click="openForm('update', scope.row.id)">
                        编辑
                    </el-button>
                    <el-button v-hasPermi="['yxt:school:delete']" link type="danger" @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total" @pagination="getList"/>
    </ContentWrap>

    <!-- 添加或修改学校对话框 -->
    <SchoolForm ref="formRef" @success="getList"/>
    <SupplierForm ref="supplierRef" @success="getList"/>
</template>

<script lang="ts" setup>
import {DICT_TYPE} from '@/utils/dict'
import {dateFormatter} from '@/utils/formatTime'
import SchoolForm from "@/views/yxt/school/SchoolForm.vue";
import {useSchoolApi} from "@/api/yxt/school";
import {useConfirm, useMessage} from "@/components/Notice";
import SupplierForm from "@/views/yxt/school/SupplierForm.vue";
import {regionData} from "element-china-area-data";

defineOptions({name: 'YxtSchool'})

const {t} = useI18n()
const schoolApi = useSchoolApi();

const loading = ref(true)
const total = ref(0)
const list = ref([])
const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    name: '',
    province: '',
})
const queryFormRef = ref()

const getList = async () => {
    loading.value = true
    try {
        const data = await schoolApi.getSchoolPage(queryParams)
        list.value = data.list
        total.value = data.total
    } finally {
        loading.value = false
    }
}

const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
}

const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id)
}

const supplierRef = ref()
const openSupplier = (id: number) => {
    supplierRef.value.open(id)
}

const handleDelete = async (id: number) => {
    useConfirm(t("common.delMessage"), async () => {
        await schoolApi.deleteSchool(id);
        getList().finally(() => useMessage(t('common.delSuccess')));
    });
}

onMounted(() => {
    getList()
})
</script>
